 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_sewa_gedung extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_tempat_model', 'Tempat');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Calon Penyewa";
		viewByTemplate('pro_tempat/index', $data, 'grid');
	}
	

	function create()
	{
		// Display Form Create
		$data['title'] = "Input Tempat Baru";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";
		$query = $this->db->query("SELECT no_toko FROM (SELECT * FROM pro_sewa_gedung ORDER BY no_toko DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['no_toko'];
		else
			$MaxID = "TK0000000"; 

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['no_toko'] = "TK".sprintf("%07s",$NextID);
		viewByTemplate('pro_tempat/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Tempat";
		$data['data'] = $this->Tempat->GetSingleView(get('no_toko'));
		
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('pro_tempat/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Tempat";
		$data['data'] = $this->Tempat->GetSingleView(get('no_toko'));
		
		$data['mode'] = "view";
		viewByTemplate('pro_tempat/form', $data, 'form');
	}


	function getdata()
	{
		$this->secure_session();
		echo $this->Tempat->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("no_toko") == '') {
			$error->Add("no_toko", "No Tenant", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM pro_sewa_gedung WHERE no_toko= '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("no"))))."'")) > 0) {
			$error->Add("no_toko", "No Tenant", $tpl_duplicate);
		}else if (post("lokasi_tempat") == '') {
			$error->Add("lokasi_tempat", "Lokasi Tempat", $tpl_empty);
		}else if (post("luas_tempat") == '') {
			$error->Add("luas_tempat", "Luas Tempat", $tpl_empty);
		}else if (post("harga_sewa") == '') {
			$error->Add("harga_sewa", "Harga Sewa", $tpl_empty);
		}else if (post("status_sewa") == '') {
			$error->Add("status_sewa", "Status Sewa", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('no_toko', 'lokasi_tempat', 'luas_tampat', 'harga_sewa', 'staus_sewa'));
				$data['no_toko']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['no_toko'])));
				
				if (post("mode") == "create")
				{
					$this->Tempat->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->Tempat->Update($data, array('no_toko'	=> $data['no_toko']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$no_toko	= get('no_toko');
		
		if ($no_toko != "")
		{
			// Delete Single
			$arr_rule	= array('no_toko' => $no_toko);
			$ls_result = $this->Tempat->DeleteSingle($arr_rule);

		}
		else
		{
		}
		
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('no_toko' => $row['no_toko']);
				$ls_result .= $this->Tempat->DeleteSingle($arr_rule);
			}
		echo $ls_result;
	}
}
?>